"use strict";
// https://gitee.com/easyatgitee/common_eggjs_app/blob/master/app/controller/sign.js
const Controller = require('./base');
const crypto = require("crypto");
class VisitController extends Controller {
 
  async list() {
    let { ctx, app } = this; 
    let { page,pageSize, title,type,uid } = ctx.request.body; 
    page = page || 1; pageSize = pageSize  || 10;
    var offset = 0;
    var offset = (page - 1) * pageSize 

    let  sql = `select count (*) as count from  visit where 1 = 1 and uid = ${uid}`
    if(title)  sql +=` and title like %${title}%`;
    if(type) sql +=` and type = ${type} `;  
    let data = await this.app.mysql.query(sql);
    let count = data[0].count; 

    sql = `select * from  visit where 1 = 1   and uid = ${uid}`
    if(title)  sql +=` and title like %${title}%`;
    if(type) sql +=` and type = ${type} `;
    sql +=` limit ${offset} , ${pageSize} ` 
    data = await this.app.mysql.query(sql); 
    ctx.apiSuccess({ count,data });
  }
  
  async visit() {
    let { ctx, app } = this; 
    let {  uid,tid,title } = ctx.request.body; 
    let res = await this.app.mysql.insert('visit',{ 
      uid,tid,title,
      created_time:this.formatDate(Date.now(),"YYYY-MM-DD HH:mm:ss"),
    })
    
    ctx.apiSuccess(res);
  } 
   
}

module.exports = VisitController;
/*
CREATE TABLE `visit`  (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `tid` int(11),
  `uid` int(11),
  `title` varchar(100),
  `created_time` datetime(0) DEFAULT NULL,  
  PRIMARY KEY (`id`) USING BTREE
); 
*/